package com.yxq.mdt.app.modules.socialcontact.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.modules.personal.bean.MainPersonalStateBean;
import com.yxq.mdt.app.modules.socialcontact.adapter.SocialStateGiveLikeAdapter;
import com.yxq.mdt.app.modules.socialcontact.adapter.SocialStateGiveListLikeAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.BrowseListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.GroupTopicListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialStateGiveLikeBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialStateGiveListLikeBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPageInfoContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPgaeInfoPresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialStateDetailsAc;
import com.yxq.mdt.app.net.ApiModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.动态详情的点赞列表
 */
public class SocialStateGiveLikeFragment extends BaseFragment<MainSocialPgaeInfoPresenter> implements MainSocialPageInfoContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.social_like_recycler)
    RecyclerView socialLikeRecycler;
    SocialStateGiveListLikeAdapter socialStateGiveLikeAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<SocialStateGiveListLikeBean.ListBean> socialStateGiveLikeBeans;
    private String topicId = "", give = "";
    SocialStateDetailsAc.OnNumberChangeListener onNumberChangeListener;

    public SocialStateGiveLikeFragment(SocialStateDetailsAc.OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    @Override
    protected int attachLayoutId() {
        setImmersionBarEnabled(false);
        return R.layout.fragment_social_state_give_like;
    }

    @Override
    protected MainSocialPgaeInfoPresenter getPresenter() {
        return new MainSocialPgaeInfoPresenter(this, new MainSocialContactModel());
    }


    @Override
    public void initView(View view) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            topicId = bundle.getString("topicId");
            give = bundle.getString("give");
        }
        socialStateGiveLikeBeans = new ArrayList<>();
        socialLikeRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        socialStateGiveLikeAdapter = new SocialStateGiveListLikeAdapter(R.layout.item_social_list_like_list, socialStateGiveLikeBeans);
        socialLikeRecycler.setAdapter(socialStateGiveLikeAdapter);
        socialStateGiveLikeAdapter.setEmptyView(R.layout.layout_empty_view, socialLikeRecycler);
        socialStateGiveLikeAdapter.isUseEmpty(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getGiveList(topicId, String.valueOf(20), String.valueOf(page));
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getGiveList(topicId, String.valueOf(20), String.valueOf(page));
        });

    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    public void refresh() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadPageInfoSuccess(MainPersonalStateBean mainPersonalStateBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadPageInfoFailed() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadTopicByIdSuccess(GroupTopicListBean groupTopicListBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadTopicByIdFailed() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadGiveFailed() {
        if (onNumberChangeListener != null) {
            onNumberChangeListener.onChange(-1, socialStateGiveListLikeBean != null ? socialStateGiveListLikeBean.getTotal() : -1);
        }
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    SocialStateGiveListLikeBean socialStateGiveListLikeBean;

    @Override
    public void onLoadGiveSuccess(SocialStateGiveListLikeBean socialStateGiveListLikeBean) {
        this.socialStateGiveListLikeBean = socialStateGiveListLikeBean;
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (page == 1) {
            socialStateGiveLikeAdapter.setNewData(socialStateGiveListLikeBean.getList());
        } else {
            if (socialStateGiveListLikeBean.getList() != null && socialStateGiveListLikeBean.getList().size() != 0) {
                socialStateGiveLikeAdapter.addData(socialStateGiveListLikeBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        socialStateGiveLikeAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent it = new Intent(mContext, SocialPersonalHomepageAc.class);
            it.putExtra("userId", socialStateGiveLikeAdapter.getItem(position).getUserId() + "");
            startActivity(it);
        });
        socialStateGiveLikeAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.social_give_attention_tv:
                    TextView attention = view.findViewById(R.id.social_give_attention_tv);
                    MainSocialContactModel.setInterestRelation(socialStateGiveLikeAdapter.getItem(position).getUserId() + "", 0, 1, new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showCenterToast("关注成功");
                            attention.setVisibility(View.GONE);
                        }

                        @Override
                        public void onError(String errMsg) {
                            showCenterToast("关注失败");
                        }
                    });
                    break;
                default:
            }
        });
        socialStateGiveLikeAdapter.isUseEmpty(!(socialStateGiveLikeAdapter.getData().size() > 0));
//        if (onNumberChangeListener != null) {
//            onNumberChangeListener.onChange(-1, socialStateGiveListLikeBean.getTotal());
//        }
    }

    @Override
    public void onLoadBrowseListFailed() {

    }

    @Override
    public void onLoadBrowseListSuccess(BrowseListBean socialStateGiveListLikeBean) {

    }


    @Override
    public void onLoadTopicFavorSuccess(String s) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadTopicFavorFailed() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }
}
